/*
 * 文 件 名:  AbastracSyncEpgService.java
 * 版    权:  Ysten Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  bingbing
 * 修改时间:  2015年3月6日
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package basic.com.liz.mongo.service.impl;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;

import basic.com.liz.mongo.service.MongoDBDao;

 

/**
 * <EPG同步抽象类>
 * 
 * @author 陆小凤
 * @version [版本号, 2015年3月6日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class AbastracSyncMongo
{
    public static final Logger logger = LoggerFactory.getLogger(AbastracSyncMongo.class);
    
    public static final int ONLIEN_STATUS = 1;
    
    public static final String EPG_TAG_COLLECTION = "epgTag";
    
    public static final String EPG_TAG_TYPE_COLLECTION = "epgClass";
    
    public static final String EPG_MATERIALTYPE_SERIES_COLLECTION = "epgClassifiedProgramSeries";
    
    public static final String EPG_TAG_ID = "tagId";
    
    // ONLINE
    public static final String ACTION_ONLINE = "ONLINE";
    
    // 修改
    public static final String ACTION_UPDATE = "UPDATE";
    
    // 修改关系
    public static final String ACTION_UPDATEREF = "UPDATE_REF";
    
    // 下线
    public static final String ACTION_OFFLINE = "OFFLINE";
    
    @Autowired
    private MongoDBDao mongoDao;
    
    public <T> void saveMongoEntity(T t, String collectionName)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Enter AbastracSyncEpgService.saveMongoEntity, mongoDBObj = " + t + ", collectionName = "
                + collectionName);
        }
        mongoDao.save(t, collectionName);
        if (logger.isDebugEnabled())
        {
            logger.debug("Exit AbastracSyncEpgService.saveMongoEntity!");
        }
    }
    
    public <T> void saveBatchMongoEntity(Collection<T> t, String collectionName)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Enter AbastracSyncEpgService.saveBatchMongoEntity, mongoDBObj = " + t + ", collectionName = "
                + collectionName);
        }
        mongoDao.insert(t, collectionName);
        if (logger.isDebugEnabled())
        {
            logger.debug("Exit AbastracSyncEpgService.saveBatchMongoEntity!");
        }
    }
    
    public <T> void removeMongoEntity(T t, String collectionName)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Enter AbastracSyncEpgService.removeMongoEntity, mongoDBObj = " + t + ", collectionName = "
                + collectionName);
        }
        mongoDao.remove(t, collectionName);
        
        if (logger.isDebugEnabled())
        {
            logger.debug("Exit AbastracSyncEpgService.removeMongoEntity!");
        }
    }
    
    public <T> void removeMongoEntity(Query query, String collectionName)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Enter AbastracSyncEpgService.removeMongoEntity del data by Query from collectionName = "
                + collectionName);
        }
        mongoDao.remove(query, collectionName);
        
        if (logger.isDebugEnabled())
        {
            logger.debug("Exit AbastracSyncEpgService.removeMongoEntity!");
        }
    }
    
    public void dropCollection(String collectionName)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("Enter AbastracSyncEpgService.dropCollection,collectionName = " + collectionName);
        }
        mongoDao.dropCollection(collectionName);
        if (logger.isDebugEnabled())
        {
            logger.debug("Exit AbastracSyncEpgService.dropCollection!");
        }
    }
    
    /**
     * 在指定集合中查询某个实体类的所有记录
     * 
     * @param collectionName
     * @return
     */
    public <T> List<T> find(Query query, Class<T> classOfT, String collectionName)
    {
        return mongoDao.find(query, classOfT, collectionName);
    }
    
    /**
     * 根据条件在指定集合中查询出一条数据
     * 
     * @param query
     * @param collectionName
     * @return
     */
    public <T> T findOne(Object id, Class<T> clazz, String collectionName)
    {
        return (T)mongoDao.findById(id, clazz, collectionName);
    }
}
